package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.custom.ActivityRegisterRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.generated.jooq.stat.tables.records.StatCollaborateRegisterRecord;
import org.jooq.TableField;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.stat.Tables.STAT_COLLABORATE_REGISTER;
import static com.tuanzi.loan.generated.jooq.stat.Tables.STAT_COMM_ACTIVITY_REGISTER;

/**
 * 通用活动注册统计
 *
 * @author yuzhenhua 2018/7/19
 */
@SuppressWarnings({"rawtypes"})
@Repository
public class ActivityRegisterRepositoryCustomImpl extends JOOQBaseRepository implements ActivityRegisterRepositoryCustom {

    @Override
    protected TableImpl getTable() {
        return STAT_COMM_ACTIVITY_REGISTER;
    }

    @Override
    public Integer getUv(Date startTime, String channelId, Integer payMode) {
        TableField<StatCollaborateRegisterRecord, Integer> count = null;
        switch (payMode) {
            case 1: // APP注册
                count = STAT_COLLABORATE_REGISTER.APP_ACTIVATE_COUNT_PHONE;
                break;
            case 2: // H5结算
                count = STAT_COLLABORATE_REGISTER.EXTERNAL_NEW_REGISTER_COUNT;
                break;
            case 7: // 登录数
                count = STAT_COLLABORATE_REGISTER.LOGIN_COUNT;
                break;
        }
        Integer uv = dslContext
                .select(DSL.sum(count))
                .from(STAT_COLLABORATE_REGISTER).where(STAT_COLLABORATE_REGISTER.CDATE.eq(new java.sql.Date(startTime.getTime())))
                .and(STAT_COLLABORATE_REGISTER.CHANNEL.eq(channelId)).fetchOneInto(Integer.class);
        return uv == null ? 0 : uv;
    }

    @Override
    public Integer getRealCount(Date settlementTime, String channelId, Integer payMode) {
        List<Integer> statCommActivityRegisterRecords = dslContext.select(DSL.sum(STAT_COMM_ACTIVITY_REGISTER.APP_ACTIVATE_COUNT_PHONE)).from(STAT_COMM_ACTIVITY_REGISTER)
                .where(STAT_COMM_ACTIVITY_REGISTER.CDATE.eq(new java.sql.Date(settlementTime.getTime()))).and(STAT_COMM_ACTIVITY_REGISTER.CHANNEL.eq(channelId))
                .groupBy(STAT_COMM_ACTIVITY_REGISTER.CHANNEL, STAT_COMM_ACTIVITY_REGISTER.CDATE).limit(1).fetchInto(Integer.class);
        Integer record = statCommActivityRegisterRecords.isEmpty() == true ? 0 : statCommActivityRegisterRecords.get(0) == null ? 0 : statCommActivityRegisterRecords.get(0);

        return record;
    }

    @Override
    public Integer getLoginRecord(Date date, String channel) {
        List<Integer> into = dslContext.select(DSL.sum(STAT_COMM_ACTIVITY_REGISTER.LOGIN_COUNT)).from(STAT_COMM_ACTIVITY_REGISTER)
                .where(STAT_COMM_ACTIVITY_REGISTER.CDATE.eq(new java.sql.Date(date.getTime()))).and(STAT_COMM_ACTIVITY_REGISTER.CHANNEL.eq(channel))
                .groupBy(STAT_COMM_ACTIVITY_REGISTER.CHANNEL, STAT_COMM_ACTIVITY_REGISTER.CDATE).fetchInto(Integer.class);
        return into.isEmpty() ? 0 : into.get(0);
    }
}
